// pages/soso/soso.js
import Toast from '../../miniprogram_npm/_vant-weapp@0.5.7@vant-weapp/toast/toast';
import Dialog from '../../miniprogram_npm/_vant-weapp@0.5.7@vant-weapp/dialog/dialog';

//搜索页面 搜索列表 加入购物车 删除搜索历史记录
import { getSosoList, setCaregoryList, setAddCart, setSearchDestroy} from '../../config/https'
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: { 
    clikType:false,       //重复点击的控制
    isvip:'',             //判断是不是vip1是0不是   
    pagesize:10,          //每页返回的数据
    sosoHotList:[],       //热门搜索数据
    sort:'0',             //判断价格的排序0没有1升2降
    type:0,               //0第一次进入1搜索为空2搜索列表
    valuesoso:'',         //搜搜内容
    sosoList:[],         //历史搜索
    good: [{name: '综合',index: 0},{name: '销量',index: 1},{name: '价格',indx: 2}],
    list:[],              //商品列表
    id:0,                 //用于判断点击态
    loadMore: false,      //加载中提示
    finished: false,      //是否已经请求完全部数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    getSosoList().then(res => { 
      console.log(res)
      this.setData({ sosoHotList: res.hot_search, sosoList: res.serach_log })
    })
  },
  //点击进入商品详情
  onpurch(e){
    wx.navigateTo({
      url: '/pages/Directsellingto/Directsellingto?id='+e.currentTarget.dataset.id,
    })
  },
  //加入购物车
  onAddCar(e) {
    var that = this
    let { id, opid } = e.currentTarget.dataset
    setAddCart({
      total: 1,
      optionid: opid,
      id: id
    }).then(res => {
      Toast("添加购物车成功~~~")
    }).catch(err => {
      Toast("添加购物车失败~~~")
    })
  },
  //点击搜索列表数据
  onList(){
    let that = this
    let sort = that.data.sort;      //0不是价格1升2降
    let order = that.data.id;       //0综合1销量2价格
    let valuesoso = that.data.valuesoso
    if(that.data.clikType){
      return
    }
    that.setData({
      loadMore:true,
      clikType:true
    })
    if(that.data.page==0){
      wx.showLoading({
        title: '加载中',
      })
    }
    if(sort == 0){
      if (order == 0){
        order = '';
        sort = '';
      } else if (order == 1){
        order = 'sales';
        sort = 'desc';
      }
    }else if(sort == 1){
      order = 'marketprice'
      sort = 'asc'
    }else if(sort == 2){
      order = 'marketprice'
      sort = 'desc'
    }
    setCaregoryList({
      page:++that.data.page,
      pagesize:that.data.pagesize,
      order: order,
      sort: sort,
      keywords: valuesoso
    }).then(res=>{
      wx.hideLoading()
      var list = res.goods
      list = [...that.data.list,...list]
      that.setData({
        loadMore: false,
        isvip:res.member_is_vip
      })
      if (list.length == 0) {
        that.setData({type:1})
      }else{
        that.setData({
          finished: res.goods.length < that.data.pagesize ? true : false,
          type: 2
        })
      }
      that.setData({
        list,
        clikType:false
      })
      wx.stopPullDownRefresh();
      
    }).catch(err=>{
      wx.hideLoading()
      Toast(err.message)
    })
  },
  //回车搜索
  onQuery(e){
    console.log(123)
    if (this.data.valuesoso == '') {
      Toast("搜索不能为空...")
      return
    }
    this.setData({page:0,list:[]})
    this.onList()
  },

  // 监听input输入
  inputBind(e){
    this.setData({
      valuesoso:e.detail.value
    })
  },
  //清空input
  onEmpty(){
    this.setData({
      type:0,
      valuesoso:''
    })
  },
  // 清空历史
  ondel(){
    
    Dialog.confirm({
      message: '确定清空当前历史搜索吗~~',
    }).then(() => {
      setSearchDestroy({}).then(res => {
        this.setData({sosoList: res})
      })
      Toast("清空成功")
    })
  },
  //点击历史搜索
  onsearch(e){
    this.setData({
      valuesoso: e.currentTarget.dataset.keyword
    })
    //调用搜索
    this.onQuery();
  },
  //点击综合销量和价格
  ongood(e){
    let sort = this.data.sort;
    let index = e.currentTarget.dataset.index
    //判断点击的是不是价格
    if(index==2){
      if(sort==0){
        sort = 1
      }else if(sort==1){
        sort = 2
      }else if(sort==2){
        sort = 1
      }
    }else{
      sort = 0
    }
    this.setData({
      id: index,
      sort,
      page:0,
      list:[]
    })
    this.onList();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.finished && !this.data.loadMore) {
      this.onList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})